package mongo

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"gopkg.in/mgo.v2"
)

func New() *mgo.Session {
	dbConfig := MongoDbConfig{
		Host:     viper.GetString("mysql.host"),
		Port:     viper.GetString("mysql.port"),
		User:     viper.GetString("mysql.username"),
		Password: viper.GetString("mysql.password"),
		DataBase: viper.GetString("mysql.database"),
	}

	return dbConfig.Connect()
}

type MongoDbConfig struct {
	Host     string
	Port     string
	User     string
	Password string
	DataBase string
}

func (c *MongoDbConfig) Connect() *mgo.Session {
	connString := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s", c.User, c.Password, c.Host, c.Port, c.DataBase)
	session, err := mgo.Dial(connString)
	if err != nil {
		logrus.Fatalln("mongoDb connect error:", err.Error())
		return nil
	}
	return session.Clone()
}
